<?php
class Session {
	
	/**
	 * Object instance
	 *
	 * @var Session
	 */
	private static $_instance;
	
	/**
	 * instance of singleton object
	 *
	 * @return Session
	 */
	public static function getInstance () {
		if(self::$_instance === null) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	/**
	 * Start session
	 *
	 */
	public function start() {
		session_start();
	}
	
	/**
	 * Destroy session 
	 *
	 */
	public function logout() {
		session_destroy();
	}

    public function delete($name) {
        if(isset($_SESSION[$name])) {
			unset($_SESSION[$name]);
		}
    }

	/**
	 * Check wether is user logged in
	 *
	 * @return boolean
	 */
	public function isLogged() {
		return $this->getBool('isLogged');
	}
	
	/**
	 * Set $_SESSION parameter
	 *
	 * @param string $name
	 * @param mixed $value
	 */
	public function set($name, $value = '') {
		$_SESSION[$name] = $value;
	}

	/**
	 * Common method for get parameter from $_SESSION
	 *
	 * @param mixed $param
	 * @param null $default
	 * @return mixed
	 */
	protected function _getSessionParam($param, $default = null) {
		if(isset($_SESSION[$param])) {
			return $_SESSION[$param];
		}
		return $default;
	}
	
	
	/**
	 * Get boolean parameter from $_SESSION
	 *
	 * @param string $param
	 * @param boolean $default
	 * @return boolean
	 */
	public function getBool($param, $default = false) {
		return (boolean)$this->_getSessionParam($param, $default);
	}
	
	/**
	 * Get integer parameter from $_SESSION
	 *
	 * @param string $param
	 * @param integer $default
	 * @return integer
	 */
	public function getInt($param, $default = 0) {
		return (int)$this->_getSessionParam($param, $default);
	}
	
	/**
	 * Get string parameter from $_SESSION
	 *
	 * @param string $param
	 * @param string $default
	 * @param boolean $trim
	 * @return string
	 */
	public function getString($param, $default = '', $trim = true, $htmlChars = true) {
		$val = (string)$this->_getSessionParam($param, $default);
		if($trim) {
			$val = trim($val);
		}
		if($htmlChars) {
			$val = htmlspecialchars($val);
		}
		return $val;
	}
	
	/**
	 * Get $_SESSION
	 *
	 * @return array
	 */
	public function getSession() {
		if(isset($_SESSION)) {
			return $_SESSION;
		}
	}
	
	public function getCurrent() {
		if(isset($_SESSION['currentUser'])) {
			return unserialize($this->_getSessionParam('currentUser'));
		}
	}

	public function checkSet($name) {
		return isset($_SESSION[$name]);
	}
}